package com.iris.live.services.data.repositories.impl;

import com.iris.live.services.data.repositories.RetailRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:车龄零售接口实现
 * @author: Tony.Zhang
 * @Date: 2017-09-15 18:17
 **/
@Repository
public class RetailRepositoryImpl extends BaseRepository implements RetailRepository {
    private static Logger logger = LoggerFactory.getLogger(RetailRepositoryImpl.class);

    public RetailRepositoryImpl() {
    }

    @Override
    public void updateSynchronizationQuotation(String quotationId) {
        StringBuffer sb = new StringBuffer();

        sb.append(" update  t_retail_base_info_increment set boss_flag = 1 where q_num=:q_num and data_type = 1");
        Map<String, Object> params = new HashMap<>();
        params.put("q_num",quotationId);

        super.executeUpdate(sb.toString(), params);
    }

    @Override
    public List getSynchronizationQuotation() {
        StringBuffer sb = new StringBuffer();

        sb.append(" select  a.q_num, b.销售顾问 sc ");
        sb.append(" from t_retail_base_info_increment a, 报价单表 b ");
        sb.append(" where a.boss_flag <> 1 and data_type = 1 and a.q_num = b.报价编号 ");

        return super.getList(sb.toString());
    }

    @Override
    public Map getVehicleInfo(String quotationId) {
        Map<String, Object> result = new HashMap<>();
        StringBuffer sb = new StringBuffer();

        sb.append(" select  UPPER(b.顾客标识) cusId, d.品牌  changPai, d.级别  cheXi, d.车型 cheXingDaiMa, c.车架号 cheJiaHao, ");
        sb.append(" 发动机号 faDongJiHao, 'N/A' chePaiHao, 'N/A' chePaiYanSe, IFNULL(颜色,'N/A') cheShenYanSe, 'N/A' bianSuXiangHao, ");
        sb.append(" 'N/A' chanDi, DATE_FORMAT(e.开票时间,'%Y-%m-%d') gouCheDate, '1900-01-01' baoXianDate, '1900-01-01' zhiZaoDate, ");
        sb.append(" IFNULL(c.内饰,'N/A')  neiShiYanSe, IFNULL(商检单号,'N/A') shangJianDanHao, IFNULL(合格证号,'N/A') heGeZhengHao, IFNULL(关单号,'N/A') jinKouZhengHao, ");
        sb.append(" 'N/A' cheLiangGuiGe, 'N/A' cheLiangSort, '1900-01-01' nextExaDate, 1 isZiDianSale, f.`人员姓名` saleName, 'N/A' saleBillNo ");
        sb.append(" from 报价单表 a, `顾客记录汇总` b, lsh库存表 c, lsh车型 d, 报价交车表 e, 用户 f , 报价订单表 g ");
        sb.append(" where a.报价编号=:quotationId and a.顾客编号 = b.顾客编号 and a.库存编号 = c.库存编号 and a.车型编号 = d.车型编号 ");
        sb.append(" and a.交车= e.编号 and a.订单编号 = g.`订单编号` and a.销售顾问 = f.用户名 ");

        Map params = new HashMap<>();
        params.put("quotationId", quotationId);
        List<Map> results = super.getList(sb.toString(), params);
        if(results != null && !results.isEmpty()){
            result = results.get(0);
        }

        return result;
    }

    @Override
    public Map getCustomerInfo(String quotationId) {
        Map<String, Object> result = new HashMap<>();
        StringBuffer sb = new StringBuffer();

        sb.append(" select UPPER(b.顾客标识) cusId, IFNULL(a.客户类型,'N/A')  cheZhuSort, b.顾客姓名 cheZhuName, IFNULL(b.电话号码,'N/A') cheZhuTel, IFNULL(b.电话号码,'N/A') cheZhuMob, ");
        sb.append(" 'N/A' cheZhuAdd, IFNULL(b.顾客性别,'N/A') cheZhuXingBie, IFNULL(c.省份,'N/A') cheZhuAreaP, IFNULL(c.城市,'N/A') cheZhuAreaC, IFNULL(c.区域,'N/A') cheZhuAreaA,  ");
        sb.append(" '1900-01-01' cheZhuJiaZhaoDate, '1900-01-01' cheZhuBirthday, IFNULL(c.身份证类型,'N/A') cheZhuCardSort, IFNULL(c.身份证,'N/A') cheZhuCardNo, IFNULL(d.售后联系人,'N/A') lianXiRenName, ");
        sb.append(" IFNULL(d.售后联系人电话,'N/A') lianXiRenTel, IFNULL(d.售后联系人电话,'N/A') lianXiRenMob, IFNULL(d.售后联系人地址,'N/A') lianXiRenAdd, 'N/A' lianXiRenXingBie,");
        sb.append(" IFNULL(d.售后联系人省,'N/A') lianXiRenAreaP, IFNULL(d.售后联系人市,'N/A') lianXiRenAreaC, IFNULL(d.售后联系人区,'N/A') lianXiRenAreaA, '1900-01-01' lianXiRenJiaZhaoDate,");
        sb.append(" '1900-01-01' lianXiRenBirthday, 'N/A' lianXiRenCardSort, 'N/A' lianXiRenCardNo, 'N/A' cheZhuSuoShuHangYe,'N/A' cheZhuGuanXiSort,'N/A' cheZhuJiaTingSort,");
        sb.append(" 'N/A' cheZhuZhiYeSort, 'N/A' cheZhuShouRu, 'N/A' cheZhuAiHao, 'N/A' cheZhuXueLi,IFNULL(c.邮箱,'NA@NA.com') cheZhuEMail,'N/A' cheZhuGongZuoDanWei,");
        sb.append(" '1900-01-01' lianXiRenFirstJiaZhaoDate, 'N/A' lianXiRenJiaZhaoArea, 'NA@NA.com' lianXiRenEMail, 'N/A' lianXiRenGongZuoDanWei,'N/A' cheZhuChengWei,'N/A' cheZhuHunYin,");
        sb.append(" '0' cheZhuZiNvQty, 'N/A' cheZhuQiYeSort, 'N/A' cheZhuMainTelSort, 'N/A' cheZhuTelSub,'N/A' cheZhuOtherTel,'N/A' cheZhuChuanZhen,");
        sb.append(" 'N/A' lianXiRenChengWei, IFNULL(d.售后联系人关系,'N/A') lianXiRenShenFen, 'N/A' lianXiRenZhiWei ");
        sb.append(" from 报价单表 a, `顾客记录汇总` b, 报价客户表 c, 报价交车表 d ");
        sb.append(" where a.报价编号=:quotationId and a.顾客编号 = b.顾客编号 and a.客户详细 = c.编号 and a.交车= d.编号 ");

        Map params = new HashMap<>();
        params.put("quotationId", quotationId);
        List<Map> results = super.getList(sb.toString(), params);
        if(results != null && !results.isEmpty()){
            result = results.get(0);
        }

        return result;
    }
}
